package com.shopping.dao;
import com.shopping.dao.provider.UpdateSelectiveProvider;
import com.shopping.pojo.Receive;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import java.util.List;

@Component
@Mapper
public interface ReceiveDao {

    String TABLE_NAME = "receive";
    String INSERT_FIELDS = "receive_name, receive_address, postcode, phone, is_default, user_id";
    String SELECT_FIELDS = "id, receive_name receiveName, receive_address receiveAddress, postcode, phone, is_default isDefault, user_id userId";

    @Insert({"insert into ", TABLE_NAME, " (", INSERT_FIELDS, ") values(#{receiveName}, #{receiveAddress}, #{postcode}, #{phone}, #{isDefault}, #{userId})"})
    int insert(Receive receive);

    @Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME, " where id=#{id}"})
    Receive selectReceive(@Param("id") int id);

    @Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME, " where user_id=#{userId}"})
    List<Receive> selectReceives(@Param("userId") int userId);

    @Delete({"delete from ", TABLE_NAME, " where id=#{id}"})
    int delete(@Param("id") int id);

    @Update({"update ", TABLE_NAME, " set receive_name=#{receive.receiveName}, receive_address=#{receive.receiveAddress}, postcode=#{receive.postcode}, " +
            "phone=#{receive.phone}, is_default=#{receive.isDefault}, user_id=#{receive.userId} where id=#{receive.id}"})
    int updateReceive(@Param("receive") Receive receive);

    @UpdateProvider(type = UpdateSelectiveProvider.class, method = "receiveUpdateSelective")
    int updateReceiverSelective(Receive receive);

    @Select({"select user_id from ", TABLE_NAME, " where id=#{receiveId}"})
    int selectUserId(@Param("receiveId") int receiveId);

    @Select({"select last_insert_id()"})
    Integer lastInsertId();
}
